package com.ken.apigateway;

import com.ken.apigateway.lancher.RequestHeaderServiceLancherHandler;
import com.ken.apigateway.lancher.ServiceLancherHandlerChain;
import feign.Request;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.ConfigurableEnvironment;

@SpringBootApplication(exclude = {
		DataSourceAutoConfiguration.class
})
@EnableDiscoveryClient
@EnableZuulProxy
@EnableFeignClients(basePackages = {"com.ken"})
@ComponentScan(basePackages = {"com.ken.apigateway"})
public class ApiGatewayApplication {

	public static void main(String[] args) {
		SpringApplication.run(ApiGatewayApplication.class, args);
	}

	@Bean
	Request.Options requestOptions(ConfigurableEnvironment env){
		int ribbonReadTimeout = env.getProperty("ribbon.ReadTimeout", int.class, 10000);
		int ribbonConnectionTimeout = env.getProperty("ribbon.ConnectTimeout", int.class, 3000);

		return new Request.Options(ribbonConnectionTimeout, ribbonReadTimeout);
	}

	@Bean
	ServiceLancherHandlerChain serviceLancherHandlerChain(){
		ServiceLancherHandlerChain chain = new ServiceLancherHandlerChain();
		chain.addHandler(new RequestHeaderServiceLancherHandler());
		return chain;
	}

}
